<?php
/*
Template Name: Register
*/
$user_ID;
if($_POST)
{
//    $emptyFields = "";
//    $username = $wpdb->escape($_REQUEST['txt_user']);
//    if(empty($username)) 
//    {  
//        $emptyFields = "User Name";
//    }  
//    $password = $wpdb->escape($_REQUEST['txt_pass']);
//    if(empty($password)) 
//    {  
//        if(empty($emptyFields))
//        {
//            $emptyFields = "Password";
//        }
//        else
//        {
//            $emptyFields = $emptyFields + ", Password";
//        }
//    }  
//    $repass = $wpdb->escape($_REQUEST['txt_repass']);
//    $fullname = $wpdb->escape($_REQUEST['txt_fullname']);
//    $address = $wpdb->escape($_REQUEST['txt_address']);
//    $GioiTinh = $wpdb->escape($_REQUEST['genre']);
//    $email = $wpdb->escape($_REQUEST['txt_email']);
//    if(empty($email)) 
//    {  
//        if(empty($emptyFields))
//        {
//            $emptyFields = "Email";
//        }
//        else
//        {
//            $emptyFields = $emptyFields + ", Email";
//        }
//    } 
//    $txt_phone = $wpdb->escape($_REQUEST['txt_phone']);
//    
//    if(!empty($emptyFields))
//    {
//        echo $emptyFields. " khong the bo trong, vui long nhap day du thong tin.";
//        exit();
//    } 
        $username = $wpdb->escape($_REQUEST['username']);  
        if(empty($username)) {  
            echo "User name should not be empty.";  
            exit();  
        }  
        $email = $wpdb->escape($_REQUEST['email']);  
        if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {  
            echo "Please enter a valid email.";  
            exit();  
        }  
  
            $random_password = wp_generate_password( 12, false );  
            $status = wp_create_user( $username, $random_password, $email );  
            if ( is_wp_error($status) )  
                echo "Username already exists. Please try another one.";  
            else {  
                $from = get_option('admin_email');  
                        $headers = 'From: '.$from . "\r\n";  
                        $subject = "Registration successful";  
                        $msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";  
                        wp_mail( $email, $subject, $msg, $headers );  
                echo "Please check your email for login details.";  
            }  
  
        exit();  
}
else
{
    // load header
    get_header();
    if(!get_option('users_can_register')) 
    {   
        echo "Hien tai khong the tao user moi duoc. Vui long lien he Admin.";
    }
?>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
<div style="margin-left: 300px;margin-top: 10px;">
    <div id="result"></div> <!-- To hold validation results -->  
    <form method="post" id="frm_register">
		<p><label>Tên đăng nhập: </label>
			<input type="text" class="txt_user" name="txt_user"/> <span class="i_required">(*)</span></p>
		<p><label>Mật khẩu: </label>
			<input type="password" class="txt_pass" name="txt_pass"/> <span class="i_required">(*)</span></p>
		<p><label>Xác nhận mật khẩu: </label>
			<input type="password" class="txt_repass" name="txt_repass"/> <span class="i_required">(*)</span></p>
		<p><label>Họ &amp; Tên: </label>
			<input type="text" class="txt_fullname" name="txt_fullname"/> <span class="i_required">(*)</span></p>
		<p><label>Địa chỉ: </label>
			<input type="text" class="txt_address" name="txt_address"/></p>
		<p><label>Giới tính </label>
			<input type="radio" class="rd_men" checked="checked" value="1" name="genre"/>Nam
			<input type="radio" class="rd_women" value="0" name="genre"/>Nữ</p>
		<p><label>Email: </label>
			<input type="text" class="txt_email" name="txt_email"/> <span class="i_required">(*)</span></p>
		<p><label>Số điện thoại: </label>
			<input type="text" class="txt_phone" name="txt_phone"/></p>
		<p>
			<input type="submit" value="Đăng ký" id="btn_register"/>
			<input type="reset" value="Xóa" id="btn_reset"/>
		</p>
        <script type="text/javascript">  
        //<![CDATA[  
          
        $("#btn_register").click(function() {  
          
        $('#result').html('<img src="<?php bloginfo('template_url') ?>/images/loader.gif" class="loader" />').fadeIn();  
        var input_data = $('#frm_register').serialize();  
        $.ajax({  
        type: "POST",  
        url:  "",  
        data: input_data,  
        success: function(msg){  
        $('.loader').remove();  
        $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');  
        }  
        });  
        return false;  
          
        });  
        //]]>  
        </script>
	</form>
</div>
<?php
}
get_footer();
?>